﻿using System;
using Mersiv.Lib.Wrapper;
using Mersiv.Lib.Wrapper.Command;
using Mersiv.Lib.Wrapper.Event;

namespace Mersiv.App.Client.Controller
{

  /// <summary>
  /// Controls the execution of HgCommand objects.
  /// </summary>
  class HgCommandController
  {

    //
    // Mercurial Commands: Shortcut functions
    //

    public void HgStatusCommand(HgRepository hgRepository)
    {
      if (hgRepository != null)
      {
        HgStatusCommand hgStatusCommand = new HgStatusCommand();
        hgStatusCommand.CommandExecutionCompleted += new HgCommandEvent(hgStatusCommand_CommandExecutionCompleted);
        hgStatusCommand.CommandErrorReceived += new HgCommandEvent(hgStatusCommand_CommandErrorReceived);
        hgStatusCommand.CommandOutputReceived += new HgCommandEvent(hgStatusCommand_CommandOutputReceived);
        hgStatusCommand.Repository = hgRepository;
        hgStatusCommand.Execute();
      }
    }

    public HgCommandController()
    {
    }


    #region Internal Event Handlers

    private void hgStatusCommand_CommandExecutionCompleted(HgCommand hgCommand, string message)
    {
      if (hgCommand != null && message != null)
      {
        this.OnCommandExecutionCompleted(hgCommand, message);
      }
    }
    private void hgStatusCommand_CommandErrorReceived(HgCommand hgCommand, string message)
    {
      if (hgCommand != null && message != null)
      {
        this.OnCommandErrorReceived(hgCommand, message);
      }
    }
    private void hgStatusCommand_CommandOutputReceived(HgCommand hgCommand, string message)
    {
      if (hgCommand != null && message != null)
      {
        this.OnCommandOutputReceived(hgCommand, message);
      }
    }

    #endregion

    #region Events

    public event HgCommandEvent CommandExecutionCompleted;
    public event HgCommandEvent CommandErrorReceived;
    public event HgCommandEvent CommandOutputReceived;

    #endregion

    #region OnEvent Methods

    protected void OnCommandExecutionCompleted(HgCommand hgCommand, string message)
    {
      if (this.CommandExecutionCompleted != null)
      {
        this.CommandExecutionCompleted(hgCommand, message);
      }
    }

    protected void OnCommandErrorReceived(HgCommand hgCommand, string message)
    {
      if (this.CommandErrorReceived != null)
      {
        this.CommandErrorReceived.Invoke(hgCommand, message);
      }
    }

    protected void OnCommandOutputReceived(HgCommand hgCommand, string message)
    {
      if (this.CommandOutputReceived != null)
      {
        this.CommandOutputReceived.Invoke(hgCommand, message);
      }
    }

    #endregion

  }

}